﻿using System.Collections.Generic;
using System.Linq;

namespace PetaPoco
{
    /// <summary>
    /// PetaPoco 批量插入扩展操作类
    /// </summary>
    public static class IDatabaseInsertBatchExtensions
    {

        /// <summary>
        /// 批量插入数据库
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="db"></param>
        /// <param name="pocos"></param>
        public static void InsertBatch<T>(this IDatabase db, IEnumerable<T> pocos)
        {
            if (!pocos.Any()) return;
            BatchProvider.Create(db).InsertBatch(pocos);
        }

        /// <summary>
        /// 批量插入数据库到指定表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableName"></param>
        /// <param name="pocos"></param>
        public static void InsertBatch(this IDatabase db, string tableName, IEnumerable<object> pocos) => InsertBatch(db, tableName, null, null, pocos);

        /// <summary>
        /// 批量插入数据库到指定表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableName"></param>
        /// <param name="primaryKeyName"></param>
        /// <param name="pocos"></param>
        public static void InsertBatch(this IDatabase db, string tableName, string primaryKeyName, IEnumerable<object> pocos) => InsertBatch(db, tableName, primaryKeyName, null, pocos);

        /// <summary>
        /// 批量插入数据库到指定表
        /// </summary>
        /// <param name="db"></param>
        /// <param name="tableName"></param>
        /// <param name="primaryKeyName"></param>
        /// <param name="autoIncrement"></param>
        /// <param name="pocos"></param>
        public static void InsertBatch(this IDatabase db, string tableName, string primaryKeyName, bool? autoIncrement, IEnumerable<object> pocos)
        {
            if (!pocos.Any()) return;
            BatchProvider.Create(db).InsertBatch(tableName, primaryKeyName, autoIncrement, pocos);
        }
    }
}
